package com.android.nearshop.app.activity.shop;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.common.TemplateActivity;
import com.android.nearshop.app.activity.main.FilterActivity;
import com.android.nearshop.app.activity.main.MainIndexActivity;
import com.android.nearshop.app.activity.search.SearchActivity;
import com.android.nearshop.app.adapter.listadapter.CommonListAdapter;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.app.widget.TitleBar.OnTitleClickListener;
import com.android.nearshop.business.location.MyBMapManager;
import com.android.nearshop.io.protocol.in.FindShopAction;
import com.android.nearshop.io.protocol.in.BannerAction.ADV_TYPE;
import com.android.nearshop.io.protocol.out.Area;
import com.android.nearshop.util.Constant;

/**
 * 优惠信息
 * @author huangyue
 *
 */
public class CheapActivity extends TemplateActivity implements OnClickListener{
	private ListView list;
	private CommonListAdapter mCommonListAdapter;
	private FindShopAction mAction;
	private TextView mTotalLabel;
	private Button mFilter;	
	// 选中的城市
    private Area mArea;
    // 商品分类的选择
    private ArrayList<Integer> mCateList;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_findshop);
		
		list = (ListView) findViewById(R.id.listView);
		mTotalLabel = (TextView)findViewById(R.id.total_label);
		mTotalLabel.setText(String.format(getString(R.string.cheap_label, "")));
		
		mFilter = (Button) findViewById(R.id.sort);
		mFilter.setVisibility(View.VISIBLE); 
		mFilter.setOnClickListener(this);
		
		new Handler().post(new Runnable() {
			@Override
			public void run() {
				initAdapter();
			}
		});
		
	}
	
	//初始化列表
	private void initAdapter() {
		if(mCommonListAdapter != null){
			return;
		}
		
		mCommonListAdapter = new CommonListAdapter(this,list);
		mCommonListAdapter.setIdisplayNetErrAndRefreshView(this);
		
		Location loc = MyBMapManager.getInstace(this).getMyLoation();
        
//        if(loc == null){
//        	loc = new Location("stander");
//        	loc.setLatitude(22.620000*1E6);
//        	loc.setLongitude(114.070000*1E6);
//        }
               
        setAction(loc);       
	}
	
    @Override
    public void onBackPressed() 
    {
        if( getParent() instanceof MainIndexActivity)
        {
            ((MainIndexActivity)getParent()).onBackPressed();
        }   	
    }
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	private void setAction(Location loc) {
		mAction = new FindShopAction(loc.getLongitude(),loc.getLatitude(),false, 1, 10);
		mAction.setOrderStruct(true, false, false, false, false, false);
		mCommonListAdapter.setAction(mAction);
		mCommonListAdapter.setToolView(findViewById(R.id.classic_button_panel));
		mCommonListAdapter.setTotalView(mTotalLabel);
		list.setAdapter(mCommonListAdapter);
		mCommonListAdapter.loadData();
	}
	
	@Override
	protected void setupTitleBar(TitleBar titleBar) 
	{
		titleBar.setLeftImage(R.drawable.icon_shake_small_w);
		titleBar.setLeftBg(R.drawable.red_button_bg);
		titleBar.setRigthImage(R.drawable.search_normal);
		titleBar.setTitle(R.string.cheap_stop);
	    titleBar.setOnTitleClickListener(new OnTitleClickListener() 
	    {
	    	//到搜索页面
            public void onRightClick() 
            {
            	 Intent intent = new Intent();
                 intent.setClass(getApplicationContext(), SearchActivity.class);
                 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                 startActivity(intent);
            }
            //返回摇一摇页面
            public void onLeftClick() 
            {
                MainIndexActivity.gotoShake(CheapActivity.this, mTitleBar.getLeftImgView());
            }
        },true, true);
	}
	
	@Override
    protected ADV_TYPE getAdvType()
    {
        // 显示广告，如果打开且这页需要显示的话
        return ADV_TYPE.SUB;
    }

	@Override
	public void onClick(View v) 
	{
	    switch(v.getId())
	    {
    	    case R.id.sort:
    	    {
    	        FilterActivity.startActivityResult(this);
    	        break;
    	    }
	    }
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
	    super.onActivityResult(requestCode, resultCode, data);
	    if(requestCode == FilterActivity.CODE_REQUEST)
        {
            if(resultCode == Activity.RESULT_OK)
            {
                mArea = (Area)data.getSerializableExtra(Constant.TAG_CITY);
                mCateList = data.getIntegerArrayListExtra(Constant.TAG_CATEGORYS);
                // 这里不做非法判断，非法就是移除了原来的条件，其实也是正确的[zhaohua]
                mAction.setAreaId(mArea == null ?  -1 : mArea.areaId);                              	
                mAction.setCatalogyIds(mCateList);
                
                mCommonListAdapter.resetPageInfo();
    			mCommonListAdapter.reload();
                mCommonListAdapter.loadData();
            }
        }
	}
}
